
const mongoose = require('mongoose')
const { baseDB} = require('./../utils/config')

const mongodbUrl= `mongodb://${baseDB.user}:${baseDB.password}@${baseDB.host}/${baseDB.dataname}`
mongoose.connect(mongodbUrl,{
    authSource: 'yirishengxian', // 指定认证数据库
})
//得到数据库连接句柄
const db = mongoose.connection
//通过数据库连接句柄，监听mongoose数据库成功的事件
db.on('open', function (err) {
    if (err) {
        console.log('数据库连接失败')
        throw err
    }
    console.log('数据库连接成功')
})
mongoose.connection.on('disconnected', () => {
    console.log('数据库断开')
})

mongoose.connection.on('error', () => {
    console.log('数据库连接异常')
})


module.exports = {
    mongoose,
    db,
}